home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 27 / CU Amiga Magazine's Super CD-ROM 27 (1998)(EMAP Images)(GB)[!][issue 1998-10].iso / CUCD / PowerPC / vbcc / machines / amigawos / libsrc / _main.c < prev    next >
C/C++ Source or Header  |  1998-08-02  |  3KB  |  104 lines

  1. /*
  2. ** _main() function for vbcc-PowerOpen/WarpOS
  3. **
  4. ** based on machines/amigappc/libsrc/_main.c (vbcc-PowerPC/SVR4)
  5. **
  6. **
  7. ** V0.3 01-Aug-98 phx
  8. **      stderr is buffered
  9. ** V0.2 06-Mar-98 phx
  10. **      <dos/dos.h> was missing
  11. **      made __WarpIsInteractive global
  12. ** V0.1 03-Mar-98 phx
  13. **      copied and adapted
  14. */
  15.  
  16. #include <stddef.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19.  
  20. #include <exec/types.h>
  21. #include <exec/execbase.h>
  22. #include <exec/libraries.h>
  23. #include <dos/dos.h>
  24. #include <powerpc/powerpc.h>
  25. #include <clib/powerpc_protos.h>
  26.  
  27.  
  28. /* from M68k startup code */
  29. extern char *_stdin,*_stdout,*_stderr;
  30. extern struct ExecBase *SysBase;
  31. extern struct Library *DOSBase;
  32.  
  33. FILE *stdin,*stdout,*stderr,*_firstfile=0,*_lastfile=0;
  34. struct __exitfuncs *__firstexit;
  35.  
  36.  
  37. extern int main(int, char *argv[]);
  38. extern void _exit(int);
  39.  
  40.  
  41.  
  42. LONG __WarpIsInteractive(BPTR file)
  43. {
  44.   struct PPCArgs pa;
  45.  
  46.   pa.PP_Code = (APTR)DOSBase;
  47.   pa.PP_Offset = -216;  /* _LVOIsInteractive */
  48.   pa.PP_Flags = pa.PP_StackSize = 0;
  49.   pa.PP_Stack = NULL;
  50.   pa.PP_Regs[PPREG_D1] = (ULONG)file;
  51.   pa.PP_Regs[PPREG_A6] = (ULONG)DOSBase;
  52.   Run68k(&pa);
  53.   return((LONG)pa.PP_Regs[PPREG_D0]);
  54. }
  55.  
  56.  
  57. void exit(int returncode)
  58. {
  59.   struct __exitfuncs *p=__firstexit;
  60.  
  61.   while(p) {
  62.     p->func();  /* execute atexit() routines */
  63.     p=p->next;
  64.   }
  65.   while(_firstfile && !fclose(_firstfile));  /* close all open files */
  66.   _freemem();  /* free all memory */
  67.   _exit(returncode);  /* low level exit */
  68. }
  69.  
  70.  
  71. void _main(int argc, char **argv)
  72. {
  73.   stdin = (FILE *)malloc(sizeof(FILE));
  74.   stdout = (FILE *)malloc(sizeof(FILE));
  75.   stderr = (FILE *)malloc(sizeof(FILE));
  76.   if(!stdin || !stdout || !stderr)
  77.     exit(EXIT_FAILURE);
  78.   stdin->filehandle = _stdin;
  79.   stdin->flags = _READABLE;
  80.   if (__WarpIsInteractive((BPTR)_stdin))
  81.     stdin->flags |= _UNBUF;
  82.   stdout->filehandle = _stdout;
  83.   stdout->flags = _WRITEABLE;
  84.   if (__WarpIsInteractive((BPTR)_stdout))
  85.     stdout->flags |= _LINEBUF;
  86.   stderr->filehandle = _stderr;
  87.   stderr->flags = _WRITEABLE;
  88.   if (__WarpIsInteractive((BPTR)_stderr))
  89.     stderr->flags |= _LINEBUF;
  90.   stdin->pointer = stdout->pointer = stderr->pointer=0;
  91.   stdin->base = stdout->base = stderr->base = 0;
  92.   stdin->count = stdout->count = stderr->count = 0;
  93.   stdin->bufsize = stdout->bufsize = stderr->bufsize = 0;
  94.   stdin->prev = 0;
  95.   stdin->next = stdout;
  96.   stdout->prev = stdin;
  97.   stdout->next = stderr;
  98.   stderr->prev = stdout;
  99.   stderr->next = 0;
  100.   _firstfile = stdin;
  101.   _lastfile = stderr;
  102.   exit(main(argc,argv));
  103. }
  104.